package org.wishwork.api.order.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wishwork.api.Api;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 订单操作日志查询
 * @author: WangYX
 * @create: 2022-04-13 15:27
 * @Version: 1.0.0
 **/
public class OrderActionQueryApi extends Api<List<OrderActionQueryResponse>, OrderActionQueryRequest> {

    private static final Logger log = LoggerFactory.getLogger(OrderActionQueryApi.class);

    private Context context;

    public OrderActionQueryApi(Context context) {
        this.context = context;
    }


    @Override
    protected String query(OrderActionQueryRequest orderActionQueryRequest) throws Exception {
        String url = "/open/order/action/query";
        Map<String, Object> params = new HashMap<>();
        params.put("o_id", orderActionQueryRequest.getOId());
        return HttpHelper.sendRequest(orderActionQueryRequest, params, url);
    }

    @Override
    protected List<OrderActionQueryResponse> process(JSONObject data, OrderActionQueryRequest orderActionQueryRequest) throws Exception {
        JSONArray datas = data.getJSONArray("datas");
        List<OrderActionQueryResponse> orderActionQueryResponses = datas.toJavaList(OrderActionQueryResponse.class);
        Boolean has_next = data.getBoolean("has_next");
        if (has_next) {
            Integer pageIndex = orderActionQueryRequest.getPageIndex();
            OrderActionQueryRequest build = orderActionQueryRequest.toBuilder().pageIndex(++pageIndex).build();
            List<OrderActionQueryResponse> business = business(build);
            orderActionQueryResponses.addAll(business);
        }
        return orderActionQueryResponses;
    }
}
